<?php
include '../data/user_dal.php';
include_once '../entity/user_entity.php';

class UserMng
{
    function update($userEntity)
    {
        $userDAL = new UserDAL();
        if (!($userDAL->validate($userEntity->id, $userEntity->email))) { echo 1;
            return false;
            exit;
        } else {
           if ($result =$userDAL->update($userEntity->id, $userEntity->email, md5($userEntity->passwd), $userEntity->telephone, $userEntity->details, $userEntity->company, $userEntity->firstName,
                                     $userEntity->lastName, $userEntity->address1, $userEntity->address2, $userEntity->city, $userEntity->country, $userEntity->province, $userEntity->zipCode,
                                     $userEntity->currency, $userEntity->jobFunction, $userEntity->hearUS, $userEntity->cadUsed, $userEntity->compenents, $userEntity->materialSpend,
                                     $userEntity->supplierType, $userEntity->lowCost, $userEntity->industry, $userEntity->employees, $userEntity->userType, $userEntity->inTime, $userEntity->inUser)) {
                                 $row = $result->fetch_row();
                                 return $row[0];
            }
        }
    }

    function delete($id)
    {
        $userDAL = new UserDAL();
        return $userDAL->delete($id);
    }

    function validate($id, $email)
    {
        $userDAL = new UserDAL();
        $result = $userDAL->validate($id, $email);
        if ($result) { echo "<script>alert('1');</script>";
            if ($result->fetch_row() > 0) {
                echo $result->fetch_row();
                return true;
            } else {
                return false;
            }
        } else {
            die("Validate error.");
        }
    }
    
    function login($email, $passwd)
    {
        $where = "email = '". $email . "' AND passwd = '" . md5($passwd) . "'";
        $userDAL = new UserDAL();
        if ($result = $userDAL->get($where)) { 
            $row  = $result->fetch_array();
            $userEntity = new UserEntity();
 
           foreach($row as $key => $value) {
               $userEntity->$key = $value;
           }
           return $userEntity;
        } 
    }

    function get($id)
    {
        $where  = "id = " . $id;
        $userDAL = new UserDAL();
        if ($result = $userDAL->get($where)) {
            $row = $result->fetch_array();
            $userEntity = new UserEntity();

           foreach($row as $key => $value) {
               $userEntity->$key = $value;
           }

            return $userEntity;
        }
    }
}

?>
